package com.kgc.kmall.kmallcloudmanagerweb;

import com.kgc.kmall.ribbon.MySelfRule;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

//自定义配置类设置负载均衡策略
//@RibbonClient(name = "KMALLCLOUD-MANAGER-SERVICE",configuration = MySelfRule.class)
//@RibbonClient(name = "KMALLCLOUD-MANAGER-SERVICE",configuration = RandomRule.class)
//使用注解的方式设置负载均衡随机策略
//@RibbonClient(name = "KMALLCLOUD-MANAGER-SERVICE",configuration = RandomRule.class)
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients("com.kgc.kmall.kmallcloudmanagerweb.service")
//熔断
@EnableCircuitBreaker
@EnableHystrix
public class KmallcloudManagerWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(KmallcloudManagerWebApplication.class, args);
    }
    //这个负载均衡是RestTemplate可以使用的
    //    @Bean
//    //开启负载均衡
//    @LoadBalanced
//    public RestTemplate restTemplate(){
//        return new RestTemplate();
//    }

    //使用注入的方式设置随机策略
//    @Bean
//    public IRule myRule(){
//        return new AvailabilityFilteringRule();
//    }

    //增加一个servlet
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }


}
